package com.yylending.las.personal.feedback;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;

import com.yylending.las.common.AESEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by HenryZhang on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class FeedbackPresenter extends Presenter<FeedbackModel, FeedbackPresenter.View> {
	private final static String TAG = "FeedbackPresenter";

	public static class Generator implements PresenterFactory<FeedbackPresenter> {
		@Override
		public FeedbackPresenter create() {
			return new FeedbackPresenter();
		}
	}

	public interface View extends IView {
		void feedbackSuccess();

		void feedbackFail(String msg);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new FeedbackModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void sendContent(String content) {
		execute(new AESEntity(new FeedbackModel.FeedBack(content)), FUNCTIONAL_CODE, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void feedBackResult(FeedbackModel.FeedbackResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				CLog.i(TAG, "rs=" + result.toString());
				mView.feedbackSuccess();
			} else {
				String msg = result.getRetMsg();
				mView.feedbackFail(TextUtils.isEmpty(msg) ? "反馈失败" : msg);
			}
		}
	}
}
